package com.letv.servlet;

import java.io.IOException;
import java.io.Writer;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.letv.common.ConnectionManager;
import com.letv.util.SQLUtil;

public abstract class AbstractWorkServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static final Logger log = LoggerFactory.getLogger(AbstractWorkServlet.class);

	public void init() throws ServletException {
		super.init();
	}

	protected abstract void work(HttpServletResponse response, Map<String, Object> params);

	public void doGet(HttpServletRequest request, HttpServletResponse response)
	        throws ServletException, IOException {

		Map<String, Object> params = new HashMap<String, Object>();

		String op = request.getParameter("op");// 关键词
		if (StringUtils.isBlank(op)) {
	        op = "";
        }
		params.put("op", op);
		try {
		    setSourceSort();
		} catch(Exception e) {
		    log.error("fail to get now source sort", e);
		}
		this.setCagegoryMapping();
		work(response,params);
	}

	/**
	 * set source mapping form leso category to vrs category
	 */
    private void setCagegoryMapping() {
        Connection connectionFromLeso = null;
        try {
            ConnectionManager connectionManager = ConnectionManager.getInstance();
            connectionFromLeso = connectionManager.getConnectionFromLeso();
        } catch (Exception e) {
            log.error("error to connect to leso", e);
            return;
        }

        try {
            SQLUtil.setCagegoryMapping(connectionFromLeso);
        } catch(Exception e) {
            log.error("error to set cagegory mapping", e);
        } finally{
            if(!SQLUtil.close(connectionFromLeso)) {
                log.error("fail to close connectionFromLeso");
            }
        }
    }

    private void setSourceSort() {
	    log.info("get now source sort" + "...");
        ConnectionManager cm = null;
        try {
            cm = ConnectionManager.getInstance();
        } catch (Exception e) {
            log.error("connection get instance error!", e);
            return;
        }
        Connection connToLeso = null;
        try {
            connToLeso = cm.getConnectionFromLeso();
            SQLUtil.setSourceSort(connToLeso);
        } catch (Exception e) {
            log.error("connection to leso error!", e);
        } finally {
            boolean r = SQLUtil.close(connToLeso);
            log.info("lesoConn close!");
            if(!r) {
                log.error("close db connToLeso error!");
            }
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
	        throws ServletException, IOException {
		doGet(request, response);
	}
	
	public void writeResponseMsg(HttpServletResponse response, String msg){
		Writer writer = null;
		try {
			writer = response.getWriter();
			writer.write(msg);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			log.error("向reponse写入消息失败",e);
		} finally {
			if (writer != null) {
				try {
					writer.close();
				} catch (IOException e) {
					log.error(e.getMessage());
					e.printStackTrace();
				}
			}
		}
	}

}
